#!/bin/bash
. /root/.bashrc
source ~/.bash_profile
source /etc/profile

export ORACLE_HOME=/home/hadoop/oracleclient/instantclient_11_2
export PATH=/home/hadoop/oracleclient/instantclient_11_2:$PATH
export ORACLE_OWNER=oracle
export ORACLE_SID=bsmp
export ORACLE_TERM=xterm
export THREADS_FLAG=native
export LD_LIBRARY_PATH=/home/hadoop/oracleclient/instantclient_11_2
export TNS_ADMIN=/home/hadoop/oracleclient/instantclient_11_2

run_home=/home/bsmp/work
workpath=${run_home}/bin/sds_v3
datapath=${run_home}/srcdata
logpath=${run_home}/log
logfile=${logpath}/sitemonitor_storage.log
database=sds/h16aug8v3w@dev111
#database=sds/h16aug8v3w@BSMPSB_GD

echo  $c_time--"sds_idc_sitemonitor_storage begin" >>${logfile}
yesterday=`date +"%Y%m%d" --date="-1 day"`

cd ${datapath}
#dos2unix *.txt

#filelist=`ls *.txt -l | grep ${yesterday} | awk '{print $9}'`
#currentfileno=`ls -l | grep txt | grep ${yesterday} | wc -l`

pwd >>${logfile}

ls FINISH*.txt>>${logfile}

filelist=`ls FINISH*.txt -l | awk '{print $9}'`
currentfileno=`ls FINISH*.txt -l| wc -l`

if [ '${currenfileno}' = '0' ]; then
        echo `date` ' No file find ! in ' ${datapath} >>${logfile}
        exit 1
else
        echo `date` ' currentfileno is ' ${currentfileno} >>${logfile}
fi

sqlplus -s ${database} << EOFa
    truncate table sds.MID_DOMAIN_PV_D.ctl;
    truncate table sds.MID_ABN_SESS_STAT_IDC_D.ctl;
disconnect;
quit;
EOFa

for filename in $filelist
do
if [ ! -z $filename ]; then
        ctlfile=MID_ABN_SESS_STAT_IDC_D.ctl
        tablename=`echo $filename|awk -F"-"  '{print $2}'`
        if [ "${tablename}" = "SDS_ABN_SESS_STAT_IDC_D" ] ; then
           ctlfile=MID_ABN_SESS_STAT_IDC_D.ctl
        else
           ctlfile=MID_DOMAIN_PV_D.ctl
        fi
        echo `date` "Control file is" ${ctlfile} >> ${logfile}

        echo  `date` "load data to db begin ...." >>${logfile}
        sqlldr userid=${database} control=${workpath}/${ctlfile} log=${logpath}/log.txt bad=${logpath}/bad.txt data=${datapath}/${filename} direct=faulse errors=100000 readsize=20971520
        echo  `date`--"load data to db end." >>${logfile}

        echo `date` "move ${datapath}/${filename} to ${run_home}/bakdata/" >> ${logfile}
        mv -f ${datapath}/${filename} ${run_home}/bakdata/
        echo "deal a file" ${filename}>>${logfile}
fi
done



echo  `date` --" sds_idc_sitemonitor_storage end" >>${logfile}


echo  "------------" >>${logfile}
echo  "------------" >>${logfile}
echo  "------------" >>${logfile}

exit 0



